package com.ruoyi.tester.executor.http;

import com.mashape.unirest.http.Headers;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
import org.json.JSONObject;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJun
 * @description TODO:
 * @date 2022/5/30 16:44
 */
public class UnirestUtil implements HttpUtils {
    private Map<String, String> getResponseHeader(Headers headers){
        Map<String, String> respHeader = new HashMap<>();
        for (String key : headers.keySet()) {
            respHeader.put(key, headers.getFirst(key));
        }
        return respHeader;
    }

    @Override
    public XzfHttpResult get(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam) {
        url=buildQueryParamUrl(url, queryParam);
        GetRequest requestWithBody = Unirest.get(url);
        if(headers != null){
            requestWithBody.headers(headers);
        }
        if(StringUtils.hasLength(contentType)){
            requestWithBody.header("Content-Type", contentType);
        }

        try {
            HttpResponse<String> httpResponse = requestWithBody.asString();
            Headers respHeaders = httpResponse.getHeaders();
            Map<String, String> responseHeader = getResponseHeader(respHeaders);
            XzfHttpResult result = new XzfHttpResult(true, httpResponse.getStatus(), httpResponse.getBody(), responseHeader);
            return result;
        } catch (UnirestException e) {
            e.printStackTrace();
            return new XzfHttpResult(false, -1, e.getMessage(), null);
        }
    }

    @Override
    public XzfHttpResult head(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam) {
        return new XzfHttpResult(false, -1, "平台head方法未实现", null);
    }

    @Override
    public XzfHttpResult post(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        try {
            url = buildQueryParamUrl(url, queryParam);
            HttpRequestWithBody requestWithBody = Unirest.post(url);
            if(headers != null){
                requestWithBody.headers(headers);
            }
            if(StringUtils.hasLength(contentType)){
                requestWithBody.header("Content-Type", contentType);
                if(contentType.contains("form")){
                    if(StringUtils.hasLength(bodyParam)) {
                        JSONObject paramJson = new JSONObject(bodyParam);
                        Map<String, Object> formMap = new HashMap<>();
                        for (String key : paramJson.keySet()) {
                            formMap.put(key, paramJson.get(key));
                        }
                        requestWithBody.fields(formMap);
                    }
                } else if(StringUtils.hasLength(bodyParam)) {
                    requestWithBody.body(bodyParam);
                }
            } else if(StringUtils.hasLength(bodyParam)) {
                requestWithBody.body(bodyParam);
            }
            HttpResponse<String> httpResponse = requestWithBody.asString();
            Headers respHeaders = httpResponse.getHeaders();
            Map<String, String> responseHeader = getResponseHeader(respHeaders);
            XzfHttpResult result = new XzfHttpResult(true, httpResponse.getStatus(), httpResponse.getBody(), responseHeader);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            return new XzfHttpResult(false, -1, e.getMessage(), null);
        }
    }

    @Override
    public XzfHttpResult put(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        try {
            url = buildQueryParamUrl(url, queryParam);
            HttpRequestWithBody requestWithBody = Unirest.put(url);
            if(headers != null){
                requestWithBody.headers(headers);
            }
            if(StringUtils.hasLength(contentType)){
                requestWithBody.header("Content-Type", contentType);
                if(contentType.contains("form")){
                    if(StringUtils.hasLength(bodyParam)) {
                        JSONObject paramJson = new JSONObject(bodyParam);
                        Map<String, Object> formMap = new HashMap<>();
                        for (String key : paramJson.keySet()) {
                            formMap.put(key, paramJson.get(key));
                        }
                        requestWithBody.fields(formMap);
                    }
                } else if(StringUtils.hasLength(bodyParam)) {
                    requestWithBody.body(bodyParam);
                }
            } else if(StringUtils.hasLength(bodyParam)) {
                requestWithBody.body(bodyParam);
            }
            HttpResponse<String> httpResponse = requestWithBody.asString();
            Headers respHeaders = httpResponse.getHeaders();
            Map<String, String> responseHeader = getResponseHeader(respHeaders);
            XzfHttpResult result = new XzfHttpResult(true, httpResponse.getStatus(), httpResponse.getBody(), responseHeader);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            return new XzfHttpResult(false, -1, e.getMessage(), null);
        }
    }

    @Override
    public XzfHttpResult delete(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        try {
            url = buildQueryParamUrl(url, queryParam);
            HttpRequestWithBody requestWithBody = Unirest.delete(url);

            if(headers != null){
                requestWithBody.headers(headers);
            }
            if(StringUtils.hasLength(contentType)){
                requestWithBody.header("Content-Type", contentType);
            }

            HttpResponse<String> httpResponse = requestWithBody.asString();
            Headers respHeaders = httpResponse.getHeaders();
            Map<String, String> responseHeader = getResponseHeader(respHeaders);
            XzfHttpResult result = new XzfHttpResult(true, httpResponse.getStatus(), httpResponse.getBody(), responseHeader);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new XzfHttpResult(false, -1, e.getMessage(), null);
        }

//        return new XzfHttpResult(false, -1, "平台delete方法未实现", null);
    }

    @Override
    public XzfHttpResult patch(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        try {
            url = buildQueryParamUrl(url, queryParam);
            HttpRequestWithBody requestWithBody = Unirest.patch(url);
            if(headers != null){
                requestWithBody.headers(headers);
            }
            if(StringUtils.hasLength(contentType)){
                requestWithBody.header("Content-Type", contentType);
                if(contentType.contains("form")){
                    if(StringUtils.hasLength(bodyParam)) {
                        JSONObject paramJson = new JSONObject(bodyParam);
                        Map<String, Object> formMap = new HashMap<>();
                        for (String key : paramJson.keySet()) {
                            formMap.put(key, paramJson.get(key));
                        }
                        requestWithBody.fields(formMap);
                    }
                } else if(StringUtils.hasLength(bodyParam)) {
                    requestWithBody.body(bodyParam);
                }
            } else if(StringUtils.hasLength(bodyParam)) {
                requestWithBody.body(bodyParam);
            }
            HttpResponse<String> httpResponse = requestWithBody.asString();
            Headers respHeaders = httpResponse.getHeaders();
            Map<String, String> responseHeader = getResponseHeader(respHeaders);
            XzfHttpResult result = new XzfHttpResult(true, httpResponse.getStatus(), httpResponse.getBody(), responseHeader);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            return new XzfHttpResult(false, -1, e.getMessage(), null);
        }
    }

    @Override
    public XzfHttpResult options(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return new XzfHttpResult(false, -1, "平台options方法未实现", null);
    }

    @Override
    public XzfHttpResult trace(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam) {
        return new XzfHttpResult(false, -1, "平台trace方法未实现", null);
    }
}
